package cn.edu.pzhu.javaweb.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.util.DruidDataSourceUtils;

import cn.edu.pzhu.javaweb.dao.UserinfoDao;
import cn.edu.pzhu.javaweb.pojo.Userinfo;
import cn.edu.pzhu.javaweb.util.DBUtil;
import cn.edu.pzhu.javaweb.util.DruidUtil;

public class UserinfoDaoImp implements UserinfoDao{

	private JdbcTemplate template = new JdbcTemplate(DruidUtil.getDataSource());
	@Override
	public int insert(Userinfo entity) {
		String sql = "insert into userinfo(username, email, gender, type) values(?, ?, ?, ?)";
		return template.update(sql, entity.getUsername(), entity.getEmail(), 
				entity.getGender() != null ? entity.getGender() : 0, entity.getType());
	}

	@Override
	public int insert(Userinfo entity, Connection con) {
		PreparedStatement pstm = null;
		int count = 0;
		try {
			// 1. 编写SQL语句，插入用户信息
			String sql = "insert into userinfo(username, email, gender, type) values(?, ?, ?, ?)";
			
			// 2. 创建预处理语句
			pstm = con.prepareStatement(sql);
			
			// 3. 设置参数
			pstm.setString(1, entity.getUsername());
			pstm.setString(2, entity.getEmail());
			pstm.setInt(3, entity.getGender() != null ? entity.getGender() : 0);
			pstm.setString(4, entity.getType());
			
			// 4. 执行插入
			count = pstm.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 5. 关闭资源
			try {
				if (pstm != null) {
					pstm.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			// 注意：这里不关闭connection，因为它是传入的参数，由调用方负责关闭
		}
		return count;
	}

	@Override
	public int deleteById(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteBatchIds(List<String> ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateById(Userinfo entity) {
		String sql="update userinfo set email=?,gender=?,type=? where username=?";
		return template.update(sql,entity.getEmail(),
			    entity.getGender() != null ? entity.getGender() : 0,
			    entity.getType(),entity.getUsername());
	}

	@Override
	public Userinfo selectById(String id) {
		String sql="select * from userinfo where username=?";
		try {
			Userinfo userinfo =template.queryForObject(sql, new BeanPropertyRowMapper<>(Userinfo.class),id);
			return userinfo;
		} catch (Exception e) {
			return null;
		}
		
		
	}

	@Override
	public List<Userinfo> selectBatchIds(List<String> ids) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Userinfo> selectAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Userinfo> selectByCondition(Userinfo condition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long selectCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long selectCountByCondition(Userinfo condition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Userinfo> selectPage(int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Userinfo> selectPageByCondition(Userinfo condition, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean existsById(String id) {
		// TODO Auto-generated method stub
		return false;
	}
}
